package cn.b.sky.mes.app;

import cn.b.sky.form.form.FormDirective;
import cn.b.sky.form.grid.GridDirective;
import cn.b.sky.user.freemarker.ButtonDirective;
import cn.b.sky.user.freemarker.JsonStringEscape;
import cn.b.sky.user.freemarker.LabelDirective;
import cn.b.sky.user.freemarker.SimpleFreeMarkerConfigurer;
import com.google.common.collect.Maps;
import freemarker.template.utility.HtmlEscape;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.Map;
import java.util.Properties;

/**
 * @Author: yueding
 * @Description:
 * @Date: Created in 15:10 2017/11/2
 * @Modified By:
 * Copyright(c) cai-inc.com
 */
@ComponentScan(basePackages = {"cn.b.sky.**"})
@ServletComponentScan(basePackages = {"cn.b.sky.admin.**"})
@SpringBootApplication
@EnableScheduling
@MapperScan({"cn.b.sky.**.dao"})
public class MesApplication {
    public static void main(String[] args) {
        SpringApplication.run(MesApplication.class, args);
    }

    @Bean
    @Primary
    public FreeMarkerConfigurer freemarkerConfig(FreeMarkerProperties properties) {
        SimpleFreeMarkerConfigurer factory = new SimpleFreeMarkerConfigurer();
        factory.setTemplateLoaderPaths(properties.getTemplateLoaderPath());
        factory.setPreferFileSystemAccess(properties.isPreferFileSystemAccess());
        factory.setDefaultEncoding(properties.getCharsetName());
        Properties settings = new Properties();
        settings.putAll(properties.getSettings());
        factory.setFreemarkerSettings(settings);
        Map<String, Object> variables = Maps.newHashMap();
        variables.put("html_escape", new HtmlEscape());
        variables.put("json_escape", new JsonStringEscape());
        variables.put("label", new LabelDirective());
        variables.put("buttonRight", new ButtonDirective());
        variables.put("customerForm", new FormDirective());
        variables.put("customerGrid", new GridDirective());
        factory.setFreemarkerVariables(variables);
        return factory;
    }
}
